home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / Kant Pro source Folder / Kant Pro 1.1 ƒ / kode new / kant build gui.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-19  |  10.9 KB  |  450 lines  |  [TEXT/MMCC]

  1. #include "kant build gui.h"
  2. #include "kant build meat.h"
  3. #include "kant build files.h"
  4. #include "dialogs.h"
  5. #include "error.h"
  6. #include "util.h"
  7. #include "main.h"
  8. #include "menus.h"
  9. #include "window layer.h"
  10. #include "text twiddling.h"
  11. #include "program globals.h"
  12.  
  13. #define kTextItem            5
  14. #define kEditRefDialog        401
  15. #define kNewRefDialog        402
  16. #define kDeleteAlert        403
  17.  
  18. enum { key_LeftArrow=0x1c, key_RightArrow, key_UpArrow, key_DownArrow };
  19.  
  20. static    Boolean DoRefDialogDispatch(Boolean isEdit, Boolean isRef, Str255 oldStr, Str255 newStr);
  21. static    pascal Boolean RefModalFilter(DialogPtr theDialog, EventRecord *theEvent, short *theItem);
  22. static    pascal Boolean InstantModalFilter(DialogPtr theDialog, EventRecord *theEvent, short *theItem);
  23. static    Boolean CheckDeleteDialogDispatch(Boolean isRef, Str255 theName);
  24.  
  25. void DoDeleteRef(WindowPtr theWindow)
  26. {
  27.     FSSpec            theFS;
  28.     TEHandle        hTE;
  29.     ControlHandle    vScrollBar;
  30.     Str255            theName;
  31.     short            selStart, selEnd;
  32.     short            lineNum;
  33.     OSErr            oe;
  34.     
  35.     if (!AnyHighlightedQQ(theWindow))
  36.         return;
  37.     
  38.     theFS=GetWindowFS(theWindow);
  39.     hTE=GetWindowTE(theWindow);
  40.     vScrollBar=GetWindowVScrollBar(theWindow);
  41.     GetHighlightedString(theWindow, theName);
  42.     
  43.     if (!CheckDeleteDialogDispatch(TRUE, theName))
  44.         return;
  45.     
  46.     if ((oe=DeleteOneReference(theFS, theName))!=noErr)
  47.     {
  48.         HandleError(kCantDeleteReference, FALSE, FALSE);
  49.         return;
  50.     }
  51.     
  52.     lineNum=CurrentLineNumber(hTE);
  53.     selStart=LineStart(hTE, lineNum);
  54.     selEnd=GetNextInstantiationOffset(theWindow);
  55.     TESetSelect(selStart, selEnd, hTE);
  56.     TEDelete(hTE);
  57.     AdjustForEndScroll(vScrollBar, hTE);
  58.     HighlightLine(hTE, lineNum);
  59.     AdjustVScrollBar(vScrollBar, hTE);
  60.     RebuildReferencesList();
  61. }
  62.  
  63. void DoDeleteInstantiation(WindowPtr theWindow)
  64. {
  65.     FSSpec            theFS;
  66.     TEHandle        hTE;
  67.     ControlHandle    vScrollBar;
  68.     Str255            theName, refName;
  69.     short            stringIndex;
  70.     short            lineNum;
  71.     OSErr            oe;
  72.     
  73.     if (!AnyHighlightedQQ(theWindow))
  74.         return;
  75.     
  76.     theFS=GetWindowFS(theWindow);
  77.     hTE=GetWindowTE(theWindow);
  78.     vScrollBar=GetWindowVScrollBar(theWindow);
  79.     GetHighlightedString(theWindow, theName);
  80.     if (!CheckDeleteDialogDispatch(FALSE, theName))
  81.         return;
  82.     
  83.     GetRefNameFromInstantiation(theWindow, refName, &stringIndex);
  84.     if ((oe=DeleteOneInstantiation(theFS, refName, stringIndex))!=noErr)
  85.     {
  86.         HandleError(kCantDeleteInstantiation, FALSE, FALSE);
  87.         return;
  88.     }
  89.     
  90.     lineNum=CurrentLineNumber(hTE);
  91.     TESetSelect(LineStart(hTE, lineNum), LineStart(hTE, lineNum+1), hTE);
  92.     TEDelete(hTE);
  93.     AdjustForEndScroll(vScrollBar, hTE);
  94.     HighlightLine(hTE, lineNum);
  95.     AdjustVScrollBar(vScrollBar, hTE);
  96. }
  97.  
  98. void DoNewRef(WindowPtr theWindow)
  99. {
  100.     FSSpec            theFS;
  101.     Str255            refName;
  102.     TEHandle        hTE;
  103.     Boolean            addWorked;
  104.     short            lineNum;
  105.     OSErr            oe;
  106.     
  107.     if (!DoRefDialogDispatch(FALSE, TRUE, "\p", refName))
  108.         return;
  109.     
  110.     theFS=GetWindowFS(theWindow);
  111.     if (ReferenceNameExistsQQ(theFS, refName))
  112.     {
  113.         HandleError(kDuplicateReferenceName, FALSE, FALSE);
  114.         return;
  115.     }
  116.     
  117.     if ((oe=AddOneReference(theFS, refName))!=noErr)
  118.     {
  119.         HandleError(kCantCreateReference, FALSE, FALSE);
  120.         return;
  121.     }
  122.     
  123.     hTE=GetWindowTE(theWindow);
  124.     lineNum=CurrentLineNumber(hTE);
  125.     TESetSelect((**hTE).teLength, (**hTE).teLength, hTE);
  126.     addWorked=AddNameToTE(hTE, refName, TRUE);
  127.     if (addWorked)
  128.         lineNum=CurrentLineNumber(hTE);
  129.     HighlightLine(hTE, lineNum);
  130.     AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
  131.     if (addWorked)
  132.         RebuildReferencesList();
  133.     else
  134.         HandleError(kModuleTooLarge, FALSE, FALSE);
  135. }
  136.  
  137. void DoNewInstantiation(WindowPtr theWindow)
  138. {
  139.     FSSpec            theFS;
  140.     Str255            instantName, refName;
  141.     TEHandle        hTE;
  142.     Boolean            addWorked;
  143.     short            dummyShort;
  144.     short            offset;
  145.     short            lineNum;
  146.     OSErr            oe;
  147.     
  148.     if (!DoRefDialogDispatch(FALSE, FALSE, "\p", instantName))
  149.         return;
  150.     
  151.     if (RefHighlightedQQ(theWindow))
  152.         GetHighlightedString(theWindow, refName);
  153.     else
  154.         GetRefNameFromInstantiation(theWindow, refName, &dummyShort);
  155.     
  156.     theFS=GetWindowFS(theWindow);
  157.     if ((oe=AddOneInstantiation(theFS, refName, instantName))!=noErr)
  158.     {
  159.         HandleError(kCantCreateInstantiation, FALSE, FALSE);
  160.         return;
  161.     }
  162.     
  163.     hTE=GetWindowTE(theWindow);
  164.     offset=GetNextInstantiationOffset(theWindow);
  165.     lineNum=CurrentLineNumber(hTE);
  166.     TESetSelect(offset, offset, hTE);
  167.     addWorked=AddNameToTE(hTE, instantName, FALSE);
  168.     if (addWorked)
  169.         lineNum=LineNumberFromOffset(hTE, offset);
  170.     HighlightLine(hTE, lineNum);
  171.     AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
  172.     
  173.     if (!addWorked)
  174.         HandleError(kModuleTooLarge, FALSE, FALSE);
  175. }
  176.  
  177. void DoEditRef(WindowPtr theWindow)
  178. {
  179.     TEHandle        hTE;
  180.     FSSpec            theFS;
  181.     Str255            oldStr, newStr;
  182.     OSErr            oe;
  183.     short            lineNum;
  184.     Boolean            addWorked;
  185.     
  186.     if (!AnyHighlightedQQ(theWindow))
  187.         return;
  188.     
  189.     hTE=GetWindowTE(theWindow);
  190.     theFS=GetWindowFS(theWindow);
  191.     GetHighlightedString(theWindow, oldStr);
  192.     
  193.     if (!DoRefDialogDispatch(TRUE, TRUE, oldStr, newStr))
  194.         return;
  195.     
  196.     if (ReferenceNameExistsQQ(theFS, newStr))
  197.     {
  198.         HandleError(kDuplicateReferenceName, FALSE, FALSE);
  199.         return;
  200.     }
  201.     
  202.     if ((oe=ReplaceOneReference(theFS, oldStr, newStr))!=noErr)
  203.     {
  204.         HandleError(kCantReplaceReference, FALSE, FALSE);
  205.         return;
  206.     }
  207.     
  208. //    ZapDrawHook(hTE);
  209.     lineNum=CurrentLineNumber(hTE);
  210.     TESetSelect(LineStart(hTE, lineNum), LineStart(hTE, lineNum+1), hTE);
  211.     TEDelete(hTE);
  212. //    RestoreDrawHook(hTE);
  213.     addWorked=AddNameToTE(hTE, newStr, TRUE);
  214.     HighlightLine(hTE, lineNum);
  215.     AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
  216.     if (addWorked)
  217.         RebuildReferencesList();
  218.     else
  219.         HandleError(kModuleTooLarge, FALSE, FALSE);
  220. }
  221.  
  222. void DoEditInstantiation(WindowPtr theWindow)
  223. {
  224.     TEHandle        hTE;
  225.     FSSpec            theFS;
  226.     Str255            oldStr, newStr, refName;
  227.     short            stringIndex;
  228.     OSErr            oe;
  229.     short            lineNum;
  230.     Boolean            addWorked;
  231.     
  232.     if (!AnyHighlightedQQ(theWindow))
  233.         return;
  234.     
  235.     hTE=GetWindowTE(theWindow);
  236.     theFS=GetWindowFS(theWindow);
  237.     GetHighlightedString(theWindow, oldStr);
  238.     GetRefNameFromInstantiation(theWindow, refName, &stringIndex);
  239.         
  240.     if (!DoRefDialogDispatch(TRUE, FALSE, oldStr, newStr))
  241.         return;
  242.     
  243.     if ((oe=ReplaceOneInstantiation(theFS, refName, stringIndex, newStr))!=noErr)
  244.     {
  245.         HandleError(kCantReplaceInstantiation, FALSE, FALSE);
  246.         return;
  247.     }
  248.     
  249. //    ZapDrawHook(hTE);
  250.     lineNum=CurrentLineNumber(hTE);
  251.     TESetSelect(LineStart(hTE, lineNum), LineStart(hTE, lineNum+1), hTE);
  252.     TEDelete(hTE);
  253. //    RestoreDrawHook(hTE);
  254.     addWorked=AddNameToTE(hTE, newStr, FALSE);
  255.     HighlightLine(hTE, lineNum);
  256.     AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
  257.     
  258.     if (!addWorked)
  259.         HandleError(kModuleTooLarge, FALSE, FALSE);
  260. }
  261.  
  262. /* the rest of these are internal to kant build gui.c */
  263.  
  264. static    Boolean DoRefDialogDispatch(Boolean isEdit, Boolean isRef, Str255 oldStr, Str255 newStr)
  265. /* returns FALSE if cancelled or unchanged */
  266. {
  267.     DialogPtr        theDialog;
  268.     short            itemSelected;
  269.     short            itemType;
  270.     Handle            item;
  271.     Rect            box;
  272.     Boolean            changed;
  273.     Str255            theStr;
  274.     ModalFilterUPP    procFilter;
  275.     short            id;
  276.     UserItemUPP        userUPP=NewUserItemProc(OutlineDefaultButton);
  277.     
  278.     RemoveHilitePatch();
  279.     
  280.     procFilter=NewModalFilterProc(isRef ? RefModalFilter : InstantModalFilter);
  281.     id=isEdit ? kEditRefDialog : kNewRefDialog;
  282.     PositionDialog('DLOG', id);
  283.     theDialog = GetNewDialog(id, 0L, (WindowPtr)-1L);
  284.     GetDItem(theDialog, 3, &itemType, &item, &box);
  285.     InsetRect(&box, -4, -4);
  286.     SetDItem(theDialog, 3, userItem, (Handle)userUPP, &box);
  287.     SetWTitle(theDialog, isEdit ? (isRef ? "\pEdit reference" : "\pEdit instantiation") :
  288.                                   (isRef ? "\pNew reference" : "\pNew instantiation"));
  289.     ParamText(isRef ? "\preference" : "\pinstantiation", "\p", isRef ? "\pname" : "\ptext", "\p");
  290.     GetDItem(theDialog, kTextItem, &itemType, &item, &box);
  291.     SetIText(item, oldStr);
  292.     SelIText(theDialog, kTextItem, 0, 32767);    /* highlight old name */
  293.     ShowWindow(theDialog);
  294.     SetPort(theDialog);
  295.     
  296.     itemSelected=0;
  297.     while ((itemSelected!=1) && (itemSelected!=2))
  298.     {
  299.         ModalDialog(procFilter, &itemSelected);
  300.     }
  301.     
  302.     changed=FALSE;
  303.     
  304.     if (itemSelected==1)
  305.     {
  306.         GetDItem(theDialog, kTextItem, &itemType, &item, &box);
  307.         GetIText(item, theStr);
  308.         
  309.         if ((isRef) && (theStr[0]==0x00))
  310.             changed=FALSE;
  311.         else
  312.         {
  313.             changed=(!Mymemcompare((Ptr)oldStr, (Ptr)theStr, oldStr[0]+1));
  314.             if (changed)
  315.             {
  316.                 Mymemcpy(newStr, theStr, theStr[0]+1);    /* return new name */
  317.             }
  318.         }
  319.     }
  320.     
  321.     HideWindow(theDialog);
  322.     DisposeDialog(theDialog);
  323.     DisposeRoutineDescriptor(procFilter);
  324.     DisposeRoutineDescriptor(userUPP);
  325.     
  326.     InstallHilitePatch();
  327.     
  328.     return changed;
  329. }
  330.  
  331. static    pascal Boolean RefModalFilter(DialogPtr theDialog, EventRecord *theEvent, short *theItem)
  332. {
  333.     unsigned char    theChar;
  334.     
  335.     switch (theEvent->what)    /* examine event record */
  336.     {
  337.         case keyDown:    /* keypress */
  338.         case autoKey:
  339.             theChar=theEvent->message & charCodeMask;    /* get ascii char value */
  340.             if ((theChar==0x0d) || (theChar==0x03))        /* RETURN or ENTER */
  341.             {
  342.                 *theItem=FakeSelect(theDialog, sfItemOpenButton);
  343.                 return TRUE;
  344.             }
  345.             else if ((theChar==0x1b) || (theChar=='`'))    /* escape or ` key */
  346.             {
  347.                 *theItem=FakeSelect(theDialog, sfItemCancelButton);
  348.                 return TRUE;
  349.             }
  350.             else if (theEvent->modifiers & cmdKey)
  351.             {
  352.                 switch (theChar)
  353.                 {
  354.                     case '.':
  355.                         *theItem=FakeSelect(theDialog, 2);
  356.                         return TRUE;
  357.                         break;
  358.                 }
  359.             }
  360.             if ((theChar>=key_LeftArrow) && (theChar<=key_DownArrow))
  361.                 return FALSE;
  362.             if (theChar==0x08)
  363.                 return FALSE;
  364.             if ((theChar>='a') && (theChar<='z'))
  365.                 return FALSE;
  366.             if ((theChar>='A') && (theChar<='Z'))
  367.                 return FALSE;
  368.             if ((theChar>='0') && (theChar<='9'))
  369.                 return FALSE;
  370.             if ((theChar=='-') || (theChar=='_'))
  371.                 return FALSE;
  372.             if (theChar==' ')
  373.             {
  374.                 theEvent->message='-';
  375.                 return FALSE;
  376.             }
  377.             
  378.             theEvent->what=nullEvent;
  379.             break;
  380.         case updateEvt:
  381.             if ((theEvent->message)!=(unsigned long)theDialog)
  382.             {
  383.                 DispatchEvents(*theEvent, FALSE);
  384.                 return TRUE;
  385.             }
  386.             break;
  387.     }
  388.     
  389.     return FALSE;    /* no faking, proceed as planned */
  390. }
  391.  
  392. static    pascal Boolean InstantModalFilter(DialogPtr theDialog, EventRecord *theEvent, short *theItem)
  393. {
  394.     unsigned char    theChar;
  395.     
  396.     switch (theEvent->what)    /* examine event record */
  397.     {
  398.         case keyDown:    /* keypress */
  399.         case autoKey:
  400.             theChar=theEvent->message & charCodeMask;    /* get ascii char value */
  401.             if ((theChar==0x0d) || (theChar==0x03))        /* RETURN or ENTER */
  402.             {
  403.                 *theItem=FakeSelect(theDialog, sfItemOpenButton);
  404.                 return TRUE;
  405.             }
  406.             else if ((theChar==0x1b) || (theChar=='`'))    /* escape or ` key */
  407.             {
  408.                 *theItem=FakeSelect(theDialog, sfItemCancelButton);
  409.                 return TRUE;
  410.             }
  411.             else if (theEvent->modifiers & cmdKey)
  412.             {
  413.                 switch (theChar)
  414.                 {
  415.                     case '.':
  416.                         *theItem=FakeSelect(theDialog, 2);
  417.                         return TRUE;
  418.                         break;
  419.                 }
  420.             }
  421.             break;
  422.         case updateEvt:
  423.             if ((theEvent->message)!=(unsigned long)theDialog)
  424.             {
  425.                 DispatchEvents(*theEvent, FALSE);
  426.                 return TRUE;
  427.             }
  428.             break;
  429.     }
  430.     
  431.     return FALSE;    /* no faking, proceed as planned */
  432. }
  433.  
  434. static    Boolean CheckDeleteDialogDispatch(Boolean isRef, Str255 theName)
  435. {
  436.     ModalFilterUPP    procFilter = NewModalFilterProc(TwoButtonFilter);
  437.     short            result;
  438.     
  439.     RemoveHilitePatch();
  440.     
  441.     PositionDialog('ALRT', kDeleteAlert);
  442.     ParamText(isRef ? "\preference" : "\pinstantiation", theName, "\p", "\p");
  443.     result=CautionAlert(kDeleteAlert, procFilter);
  444.     DisposeRoutineDescriptor(procFilter);
  445.     
  446.     InstallHilitePatch();
  447.     
  448.     return (result==1);
  449. }
  450.